package com.ymttest.business.service;

import java.math.BigDecimal;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.testcase.Ts_PlaceOrder;
import com.ymt.core.tool.Logger;

public class Trading {

	private OrderCallService orderService = new OrderCallService();

	/**
	 *交易全流程 
	 */
	public void trading() {
		try {
			// 创建订单

			PlaceOrderResp placeOrderResp = placeOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int orderId = order.getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal payAmount = placeOrderResp.getPayAmount();

			/*			Thread.sleep(3000);

						orderService.cancelOrder(orderId, userId,
								CancelOrderTypeEnum.BuyerCancelOrder);*/

			// 买家付款
			orderService.orderPayRequest(userId, payAmount, tradingId);
			// 接单
			orderService.acceptOrder(orderId, sellerId);

			//Logger.info("orderId:" + orderId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PlaceOrderResp placeOrder() throws JSONException {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		JSONObject pro = Ts_PlaceOrder.creaderPro(100);

		JSONObject pro2 = Ts_PlaceOrder.creaderPro(100);

		placeorderBean = PlaceOrderService.getDefaultOrderReq();

		OrderGroupVo orderGroupVo = placeorderBean.getOrderGroups().get(0);
		//平台券
		//placeorderBean.setYmatouCoupon(null);
		//商家券
		//orderGroupVo.setSellerCoupon(null);

		OrderItemVo orderItemVo = placeorderBean.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems().get(0);

		orderItemVo.setProductId(pro.getString("ProductId"));

		orderItemVo.setCatalogId(pro.getJSONArray("CatalogList")
				.getJSONObject(0).getString("CatalogId"));

		orderItemVo.setProductNum(2);
		//OrderItemVo.setFreight(BigDecimal.ZERO);

		orderItemVo.setLogisticsType(LogisticsTypeEnum.SailProtect);

		OrderItemVo orderItemVo2 = PlaceOrderService.getDefaultOrderItemVo();

		orderItemVo2.setProductId(pro2.getString("ProductId"));

		orderItemVo2.setCatalogId(pro2.getJSONArray("CatalogList")
				.getJSONObject(0).getString("CatalogId"));
		orderItemVo2.setProductNum(2);

		//orderItemVo2.setFreight(BigDecimal.ZERO);

		orderItemVo2.setLogisticsType(LogisticsTypeEnum.SailProtect);

		orderGroupVo.getOrders().get(0).getOrderItems().add(orderItemVo2);

		placeorderBean.setNeedsReduceStock(true);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}

	/**
	 *  创建一个订单 
	 * @param isBargainGroup 是否是砍价团
	 * @param isUserCoupon 是否使用优惠券
	 * @return
	 */
	public static TradingServiceResp placeOrder1(boolean isBargainGroup) {

		com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq(isBargainGroup);

		//买手1 订单组
		com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo groupVo1 = placeOrderReq
				.getOrderGroups().get(0);

		List<com.ymatou.iapi.optrade.parameter.vo.OrderVo> orders = groupVo1
				.getOrders();

		for (int i = 0; i < 10; i++) {

			orders.add(TradingCallService.createOrderVo("xxxxxxx"));
		}

		return TradingCallService.callPlaceOrderService(placeOrderReq);

	}
	public static void main(String[] args) {

		Trading t = new Trading();

		Logger.setDebug(false);

		//多少个线程
		for (int i = 0; i < 1; i++) {
			Logger.info("******************Thread " + (i + 1)
					+ "*******************");
			new Thread(() -> {
				try {
					//单个线程跑多少次
					for (int j = 0; j < 10; j++) {
						Logger.info("******************" + j
								+ "*******************");
						//t.trading();
						//TradingCallService.placeOrder1(true, false);

						/*		Logger.info("getMainOrderId:"
										+ placeOrder1(true).getMainOrderId());*/

						Logger.info("getMainOrderId:"
								+ TradingCallService.placeOrder1(false, false)
										.getMainOrderId());

					}
				} catch (Exception e) {

					e.printStackTrace();
				}
			}).start();

		}

	}
}
